import 'package:flutter/material.dart';

import '../config/index.dart';

/// 获取当前主题资源
ThemeResources get themeResources => ThemeManager().currentThemeResources;

/// 获取当前主题颜色
ThemeColors get themeColors => themeResources.colors;

/// 获取当前主题图片
ThemeImages get themeImages => themeResources.images;

/// 是否是暗黑模式
bool get isDarkMode => themeResources.isDarkMode;

class ThemeManager extends ValueNotifier<ThemeData> {
  // 单例模式
  static final ThemeManager _instance = ThemeManager._internal();
  factory ThemeManager() => _instance;

  // 私有构造函数，初始值为亮色主题
  ThemeManager._internal() : super(themeDataLight) {
    _currentThemeResources = ThemeResources.light;
  }

  // 当前主题资源
  ThemeResources _currentThemeResources = ThemeResources.light;

  // 获取当前主题资源
  ThemeResources get currentThemeResources => _currentThemeResources;

  // 当前是否是暗黑模式
  bool get isDarkMode => value.brightness == Brightness.dark;

  // 切换主题
  void toggleTheme() {
    if (isDarkMode) {
      setLightTheme();
    } else {
      setDarkTheme();
    }
  }

  // 设置为亮色主题
  void setLightTheme() {
    value = themeDataLight;
    _currentThemeResources = ThemeResources.light;
  }

  // 设置为暗色主题
  void setDarkTheme() {
    value = themeDataDark;
    _currentThemeResources = ThemeResources.dark;
  }

  // 创建自定义主题
  void createCustomTheme({
    required ThemeData themeData,
    required ThemeResources themeResources,
  }) {
    value = themeData;
    _currentThemeResources = themeResources;
  }
}
